/* $Id:record.h $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#ifndef __RECORD_H
#define __RECORD_H

#include <string>
#include <map>
#include <list>
using namespace std;

#include "field.h"

#define loopRecordFields(r,v) loopSingle((r)->listFields(),v,class field,list)

class record {
    string name;
    map<string,class field> fields;
    int maxFields;
    bool relations; // true when this records has relations from other records pointed to it
    bool main; // true when this record has it's own repository to directly retrieve it, it isn't a normal child
    bool named; // true when this record contains a name field
    bool ordered; // true when this record contains a nr field, loops through these records are ordered on this nr
    string idField; // when filled this record uses named or nr fields from the related records that the field with this name points to
public:
    record();

    // Field : name
    const string getName() const;
    void setName(const string v);

    // Field : fields
    class field& getFields(const string v);
    list<class field>& listFields();
    void setFields(class field &v);
    void delFields(const string v);
    const bool isFields(const string v) const;

    // Field : relations
    const bool getRelations() const;
    void setRelations(const bool v);

    // Field : main
    const bool getMain() const;
    void setMain(const bool v);

    // Field : named
    const bool getNamed() const;
    void setNamed(const bool v);

    // Field : ordered
    const bool getOrdered() const;
    void setOrdered(const bool v);

    // Field : idField
    const string getIdField() const;
    void setIdField(const string v);

    void xml(ofstream &f, int l=0, int r=0);
};

// Repository

#define loopRecords(v) loopMap(record_repo.listRecord(),v,class record)

class recordRepository {
    map<string,record> records;
public:
    class record& getRecord(const string v);
    void setRecord(class record& v);
    map<string,class record>& listRecord();
    void delRecord(const string v);
    const bool isRecord(const string v) const;
};

extern recordRepository record_repo;

#endif // __RECORD_H
